R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

#SEE PACKAGE MAPVIEW #SLIDE 8 #opts_chunk : echo, eval TRUE, message, warning FALSE. fig.retina = 3

P = npts(gps_path, byb_feature = TRUE)

gps_path2 <- cbind (gps_path,p)

#maybe can use community detection to find the relationship

loyalty_data <- read_csv("MC2/loyalty_data.csv")
## 
## -- Column specification --------------------------------------------------------------------------------------------------------------------------------------------
## cols(
##   timestamp = col_character(),
##   location = col_character(),
##   price = col_double(),
##   loyaltynum = col_character()
## )
loyalty_data$timestamp <- date_time_parse(loyalty_data$timestamp,
                                zone = "",
                                format = "%m/%d/%Y")

cc_data <- read_csv("MC2/cc_data.csv")
## 
## -- Column specification --------------------------------------------------------------------------------------------------------------------------------------------
## cols(
##   timestamp = col_character(),
##   location = col_character(),
##   price = col_double(),
##   last4ccnum = col_double(),
##   id = col_double()
## )
cc_data$timestamp_date <- date_time_parse(cc_data$timestamp,
                                zone = "",
                                format = "%m/%d/%Y")
cc_data$timestamp <- date_time_parse(cc_data$timestamp,
                                zone = "",
                                format = "%m/%d/%Y %H:%M")



cc_data
## # A tibble: 1,490 x 6
##    timestamp           location            price last4ccnum    id timestamp_date     
##    <dttm>              <chr>               <dbl>      <dbl> <dbl> <dttm>             
##  1 2014-01-06 07:28:00 Brew've Been Served 11.3        4795    34 2014-01-06 00:00:00
##  2 2014-01-06 07:34:00 Hallowed Grounds    52.2        7108    12 2014-01-06 00:00:00
##  3 2014-01-06 07:35:00 Brew've Been Served  8.33       6816    20 2014-01-06 00:00:00
##  4 2014-01-06 07:36:00 Hallowed Grounds    16.7        9617    18 2014-01-06 00:00:00
##  5 2014-01-06 07:37:00 Brew've Been Served  4.24       7384    17 2014-01-06 00:00:00
##  6 2014-01-06 07:38:00 Brew've Been Served  4.17       5368    NA 2014-01-06 00:00:00
##  7 2014-01-06 07:42:00 Coffee Cameleon     28.7        7253     6 2014-01-06 00:00:00
##  8 2014-01-06 07:43:00 Brew've Been Served  9.6        4948    NA 2014-01-06 00:00:00
##  9 2014-01-06 07:43:00 Brew've Been Served 16.9        9683    33 2014-01-06 00:00:00
## 10 2014-01-06 07:47:00 Hallowed Grounds    16.5        8129    NA 2014-01-06 00:00:00
## # ... with 1,480 more rows
gps <- read_csv("MC2/gps.csv")
## 
## -- Column specification --------------------------------------------------------------------------------------------------------------------------------------------
## cols(
##   Timestamp = col_character(),
##   id = col_double(),
##   lat = col_double(),
##   long = col_double()
## )
gps$Date <-date_time_parse(gps$Timestamp,
                                zone = "",
                                format = "%m/%d/%Y")
gps$Timestamp <-date_time_parse(gps$Timestamp,
                                zone = "",
                                format = "%m/%d/%Y %H:%M:%S")
gps_sort <- gps[order(gps$Timestamp, gps$id),]
stop_fin = data.frame()

for (id_1 in unique(gps_sort$id))
{
  make_stop = data.frame()
  gps_id <- gps_sort %>%
  filter(id==id_1)
  
  for(i in 1:nrow(gps_id))
  {
    if(i < nrow(gps_id))
    {
      start <- gps_id$Timestamp[i]
      end <- gps_id$Timestamp[i+1]
    } else
    {
      start <- gps_id$Timestamp[i]
      end <- gps_id$Timestamp[i]
    }
    elapsed.time <- start%--%end
    time_duration <- as.duration(elapsed.time)
    
    if(time_duration > 120)
    {
      make_stop = rbind(make_stop,cbind(gps_id[i,],time_duration,i),cbind(gps_id[i+1,],time_duration,i))
    }
    
  }
  
  stop_fin = rbind(stop_fin, make_stop)
}
Abila_st <- st_read(dsn = "MC2/Geospatial",
                    layer = "Abila")
## Reading layer `Abila' from data source 
##   `C:\Users\jovinkahartanto\Desktop\SMU MITB-JOVINKA\May 2021 - Aug 2021\Visual Analytics\Assignment\Assignment-ISSS608\MC2\Geospatial' using driver `ESRI Shapefile'
## Simple feature collection with 3290 features and 9 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: 24.82401 ymin: 36.04502 xmax: 24.90997 ymax: 36.09492
## Geodetic CRS:  WGS 84
P <- npts(Abila_st, by_feature = TRUE)
abila_st_2 <- cbind(Abila_st,P) %>% filter(P > 1)

gps_sf <- st_as_sf(stop_fin,
                   coords = c("long","lat"),
                   crs=4326)

gps_path <- gps_sf %>%
  group_by(id, i) %>%
  summarize(m_Timestamp = mean(Timestamp),
            do_union = FALSE ) %>%
  st_cast("LINESTRING")
## `summarise()` has grouped output by 'id'. You can override using the `.groups` argument.
gps_path_selected <- gps_path %>%
  filter(id==5)


bgmap <- raster("MC2/Geospatial/MC2-tourist.TIF")

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(bgmap) +
  tm_rgb(bgmap, r = 1, g = 2, b = 3,
         alpha = NA,
         saturation = 1,
         interpolate = TRUE,
         max.value = 255) +
  tm_shape(abila_st_2) +
  tm_lines(col = "red", scale = 1)+
  tm_shape(gps_path_selected[c(2,8,13,18,20),]) +
  tm_lines(col = "black", scale =7, interactive = TRUE) +
  tm_text("i", size = 4, remove.overlap = TRUE, overwrite.lines = TRUE, just = "top")
## stars object downsampled to 1303 by 768 cells. See tm_shape manual (argument raster.downsample)
## Warning: col specification in tm_raster is ignored, since stars object contains a 3rd dimension, where its values are used to create facets
cc_selected <- cc_data %>% filter(location == "Abila Zacharo")

cc_selected$timestamp <- as_date_time(cc_selected$timestamp)

sf_selected = stop_fin %>% filter(id == 35, i == 682)


plot_id = data.frame()

for(id1 in unique(stop_fin$id))
{
  potential_id = data.frame()
  sf_selected_1 = stop_fin %>% filter(id == id1)
  for (i1 in unique(sf_selected_1$i))
  {
    sf_selected_2 = stop_fin %>% filter(id == id1, i == i1)
     
    if(cc_selected$timestamp[72] >= sf_selected_2[1,1] & cc_selected$timestamp[72] <= sf_selected_2[2,1])
    {
       potential_id = rbind(potential_id, sf_selected_2[1:2,])
    } 
  }
  
  plot_id = rbind(plot_id,potential_id)
}

gps_sf <- st_as_sf(plot_id,
                   coords = c("long","lat"),
                   crs=4326)

gps_path <- gps_sf %>%
  group_by(id, i) %>%
  summarize(m_Timestamp = mean(Timestamp),
            do_union = FALSE ) %>%
  st_cast("LINESTRING")
## `summarise()` has grouped output by 'id'. You can override using the `.groups` argument.
gps_path_selected <- gps_path

bgmap <- raster("MC2/Geospatial/MC2-tourist.TIF")

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(bgmap) +
  tm_rgb(bgmap, r = 1, g = 2, b = 3,
         alpha = NA,
         saturation = 1,
         interpolate = TRUE,
         max.value = 255) +
  tm_shape(abila_st_2) +
  tm_lines(col = "red", scale = 1)+
  tm_shape(gps_path_selected) +
  tm_lines(col = "black", scale =7, interactive = TRUE) +
  tm_text("id", size = 4, remove.overlap = TRUE, overwrite.lines = TRUE, just = "top")
## stars object downsampled to 1303 by 768 cells. See tm_shape manual (argument raster.downsample)
## Warning: col specification in tm_raster is ignored, since stars object contains a 3rd dimension, where its values are used to create facets
car_assignments <- read_csv("MC2/car-assignments.csv")
## 
## -- Column specification --------------------------------------------------------------------------------------------------------------------------------------------
## cols(
##   LastName = col_character(),
##   FirstName = col_character(),
##   CarID = col_double(),
##   CurrentEmploymentType = col_character(),
##   CurrentEmploymentTitle = col_character()
## )
car_assignments
## # A tibble: 44 x 5
##    LastName       FirstName CarID CurrentEmploymentType  CurrentEmploymentTitle
##    <chr>          <chr>     <dbl> <chr>                  <chr>                 
##  1 Calixto        Nils          1 Information Technology IT Helpdesk           
##  2 Azada          Lars          2 Engineering            Engineer              
##  3 Balas          Felix         3 Engineering            Engineer              
##  4 Barranco       Ingrid        4 Executive              SVP/CFO               
##  5 Baza           Isak          5 Information Technology IT Technician         
##  6 Bergen         Linnea        6 Information Technology IT Group Manager      
##  7 Orilla         Elsa          7 Engineering            Drill Technician      
##  8 Alcazar        Lucas         8 Information Technology IT Technician         
##  9 Cazar          Gustav        9 Engineering            Drill Technician      
## 10 Campo-Corrente Ada          10 Executive              SVP/CIO               
## # ... with 34 more rows
# gps_path_daily <- gps_sf %>%
#   group_by(id,Timestamp_Date) %>%
#   summarize(m = mean(Timestamp_Date),
#             do_union = FALSE ) %>%
#   st_cast("LINESTRING")
# gps_path_daily_selected <- gps_path_daily %>%
#   filter(id==1 & Timestamp_Date == "2014-01-06")
# 
# bgmap <- raster("MC2/Geospatial/MC2-tourist.TIF")
# 
# tmap_mode("view")
# tm_shape(bgmap) +
#   tm_rgb(bgmap, r = 1, g = 2, b = 3,
#          alpha = NA,
#          saturation = 1,
#          interpolate = TRUE,
#          max.value = 255) +
#   tm_shape(gps_path_daily_selected) +
#   tm_lines()
# gps1 <- gps_sf %>% 
#         filter(id == 1 & Timestamp > "2014-01-06 07:57:01" & Timestamp < "2014-01-06 07:57:48")
# 
# gps_path1 <- gps1 %>%
#   group_by(id) %>%
#   summarize(m = mean(Timestamp),
#             do_union = FALSE ) %>%
#   st_cast("LINESTRING")
# 
# 
# tmap_mode("view")
# tm_shape(bgmap) +
#   tm_rgb(bgmap, r = 1, g = 2, b = 3,
#          alpha = NA,
#          saturation = 1,
#          interpolate = TRUE,
#          max.value = 255) +
#   tm_shape(gps_path1) +
#   tm_lines()